/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ListeningThread;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author THANG
 */
public class ListeningThread extends Thread {
            /*private ServerSocket  e; // dung tcp
            private Socket f; //dung tcp*/
    private DatagramSocket socket;//dung udgp
    private InetAddress address;
    private String request;
    private boolean stillRuning = true;
    
    public   ArrayList<String> ChunkName;  
    private String File;
    
    public ListeningThread() throws UnknownHostException, SocketException
    {
        this.address = InetAddress.getByName("127.0.0.1");
        this.request = "String"; 
        this.socket = new DatagramSocket(1000);
        this.ChunkName = new   ArrayList<String> (); 
        
    }
    
    public ListeningThread(int port ) throws UnknownHostException, SocketException
    {
        this.address = InetAddress.getByName("127.0.0.1");
        this.request = ""; 
        this.socket = new DatagramSocket(port);
        this.ChunkName = new   ArrayList<String> ();
        
    }
     public String[] ReadFileName()
    {
        File folder = new File("D:\\HOAN CHINH\\Hoc Ky 2\\java\\torrent\\temp");
        String[] str = new String[folder.listFiles().length];
        int i =0;
        for (final File fileEntry : folder.listFiles()) 
        {
            str[i] = fileEntry.getName();
            i++;
        }
        return str;
    }
     public void Send(File file, DatagramSocket socket)
    {
        byte[] buff = new byte[1024];
            try {
                FileInputStream f = new FileInputStream(file);
                f.read(buff);
            } catch (FileNotFoundException ex) {
               
            } catch (IOException ex) {
                
            }
        //   buff = this.data.getBytes();
            DatagramPacket p = new DatagramPacket(buff, buff.length, this.address, 1005);
            try {
                socket.send(p);
            } catch (IOException ex) {
               
            }
    }
    @Override
    public void run()
    {
        while(stillRuning)
        {
            byte[] buff = new byte[1024];
            DatagramPacket p = new DatagramPacket(buff, buff.length);
            try {
                socket.receive(p);
            } catch (IOException ex) {
                Logger.getLogger(ListeningThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            String filename = new String(p.getData(),0,p.getLength());
            ChunkName.add(filename);
            String[] chuoi = ReadFileName();
            for(int i = 0; i < chuoi.length; i++ )
            {
                
            if(filename.equals(chuoi[i]))
            {
                System.out.printf("co file " + filename);
                File chunk = new File(" temp\\ " +filename);
                Send(chunk,socket);
            }
            }
            
            
            //System.out.printf(str);
            this.stillRuning = false;
        }
    }

    
   /* public boolean CheckChunkName()
    {
        String[] str = ReadFileName();
        for(int i = 0; i < str.length; i++ )
        {
            if(ChunkName.get(i) == str[i])
            {
                return true;
            }
        }
        return false;
    }*/
    public ArrayList GetArr()
    {
        return ChunkName;
    }
}
